package Laborum2.repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import Laborum2.Basicas.Estudios;

@Component
public class JdbcEstudiosdRepository implements EstudiosDRepository {

	@Autowired
	private DataSource dataSource;
	
	public Estudios es =new Estudios();
	List <Estudios> est=new ArrayList <Estudios>();
	
	@Override
	public Estudios obtenerEstudiosd(String nombre) {
		 String sql = "SELECT * from estudios where desEst=?" ;
			
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			try {
				conn = dataSource.getConnection();
				ps = conn.prepareStatement(sql);
				ps.setString(1, nombre);
			
				rs = ps.executeQuery();
				
				while (rs.next()) {	
					es =new Estudios();
					es.setId(rs.getInt("id"));
					es.setDescripcion(rs.getString("desEst"));
					es.setEstado(rs.getInt("desEst"));
				
				est.add(es);    

			   }
				
	           
			} catch (SQLException ex) {

			}
			closeConecction(rs, ps, conn);
	
	return es;

	}

	private void closeConecction(ResultSet rs, PreparedStatement ps,Connection conn) {

		if (rs != null) {
			try {
				// Close to prevent database cursor exhaustion
				rs.close();
			} catch (SQLException ex) {
			}
		}
		if (ps != null) {
			try {
				// Close to prevent database cursor exhaustion
				ps.close();
			} catch (SQLException ex) {
			}
		}
		if (conn != null) {
			try {
				// Close to prevent database connection exhaustion
				conn.close();
			} catch (SQLException ex) {
			}
		}

	}
}
